﻿using Microsoft.VisualBasic;

namespace Faser_Shop.Utils
{

    /// <summary>
    /// 错误码
    /// [错误类型:1][模块码:2][具体错误码:2]
    /// </summary>
    public class ErrorCodes
    {
        // Code: 1xxxx 模型验证失败
        public const int BASE_MODEL_VALIDATION_FAILED = 10000;
        // Code: 2xxxx 数据库操作失败
        public const int BASE_DB_OPERATION_FAILED = 20000;
        // Code: 3xxxx 业务逻辑错误
        public const int BASE_BUSINESS_LOGIC_ERROR = 30000;

        // Code: -1    服务器内部异常
        public const int SERVER_INTERNAL_ERROR = -1;
    }

    public class ApiResult
    {
        static public object SUCCESS = Success(null);

        static public object USER_NOT_FOUND = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "用户不存在或已注销", null);

        static public object REGISTER_FAILED_DB_NOCHANGE = Error(ErrorCodes.BASE_DB_OPERATION_FAILED, "注册失败: 数据未能成功变更", null);

        static public object LOGIN_FAILED_USER_NOTFOUND = Error(ErrorCodes.BASE_DB_OPERATION_FAILED, "登录失败: 账号或密码错误", null);

        static public object UPDATE_FAILED_DB_NOCHANGE = Error(ErrorCodes.BASE_DB_OPERATION_FAILED, "更新失败: 数据未发生变更", null);

        static public object INSERT_FAILED_DB_NOCHANGE = Error(ErrorCodes.BASE_DB_OPERATION_FAILED, "添加失败: 数据未发生变更", null);

        public static object DELETE_FAILED_DB_NOCHANGE = Error(ErrorCodes.BASE_DB_OPERATION_FAILED, "删除失败: 数据未发生变更", null);

        static public object PRODUCT_NOT_FOUND = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "商品不存在", null);

        static public object SHOPPING_CART_IS_EMPTY = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "购物车为空，没有可结算的商品", null);

        static public object SHOPPING_CART_NOT_FOUND = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "购物车中不存在该商品", null);

        static public object ORDER_NOT_FOUND = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "订单不存在", null);

        static public object INVALID_EXPRESS = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "无效的发货网点", null);

        static public object PRODUCT_STOCK_NOT_ENOUGH = Error(ErrorCodes.BASE_BUSINESS_LOGIC_ERROR, "商品库存不足", null);

        static public object ServerInternalError(object data)
        {
            return Error(ErrorCodes.SERVER_INTERNAL_ERROR, "服务器内部错误", data);
        }

        static public object ModelValidationFailed(string msg, object data)
        {
            return Error(ErrorCodes.BASE_MODEL_VALIDATION_FAILED, $"不合法的输入: {msg}", data);
        }

        static public object Build(int code, string msg, object data)
        {
            return new
            {
                code = code,
                msg = msg,
                data = data
            };
        }

        static public object Success(object data)
        {
            return Build(0, "", data);
        }

        static public object Error(int code, string msg, object data)
        {
            return Build(code, msg, data);
        }
    }
}
